<?php

namespace Home\Controller;

use Common\Controller\CommonController;

class RoleController extends CommonController
{
    protected $article = null;

    public function _initialize()
    {
        $this->user = D('User');
        $this->role_user = D('Role');
        $this->node = D('Node');
        $this->access = D('Access');
    }

    //列表
    public function lists()
    {
        $opt = [];
        $page = I('page', 1);
        $pageSize = 20;
        $data = $this->role_user->getAllByLimit($opt, $page, $pageSize);

        $pageData['pageInfo'] = pagination($page, $data['count'], $pageSize);
        $this->assign('data', $data);
        $this->assign('pageData', $pageData);
        $this->display();
    }


    //新增
    public function add()
    {
        $data = $this->role_user->getAll();
        $data = $this->tree($data,0);
        $node_list = $this->node->getAll();
        $node_list = $this->trees($node_list,0);
        $length = ceil (count($node_list) / 6);
        $this->assign('data',$data);
        $this->assign('node_list',$node_list);
        $this->assign('length',$length);
        $this->display();
    }


    public function add_callback()
    {
        $node_id_list = I('node_id');
        if (!$this->role_user->create()) {
            $this->ajaxReturn(array('msg' => $this->role_user->getError(), 'error_code' => 1));
        }
        $id = $this->role_user->add();
        if(!$id){
            $this->ajaxReturn(array('msg' => $this->role_user->getError(), 'error_code' => 1));
        }
        foreach($node_id_list as $key=>$value){
            $this->access->node_role_add($key,$id);
        }
        $this->ajaxReturn(array('msg' => '新增成功！', 'error_code' => 0));
    }



    public function edit()
    {
        $id = I('id');
        $row = $this->role_user->where("id={$id}")->find();
        $this->assign('row', $row);
        $this->display();
    }

    public function edit_callback()
    {
        $id = I('id');
        $password = I('password');
        $role_id = I('role_id');
        if (!$this->role_user->create()) {
            $this->ajaxReturn(array('msg' => $this->role_user->getError(), 'error_code' => 1));
        }
        if($password){
            $this->role_user->password = md5($password);
        }

        $this->role_user->where("id={$id}")->save();
        $this->role_user->role_user_update($role_id, $id);
        $this->ajaxReturn(array('msg' => '修改成功！', 'error_code' => 0));
    }

    public function del_callback()
    {
        $id = I('id');
        $this->role_user->where("id={$id}")->delete();
        $this->ajaxReturn(array('msg' => '删除成功！', 'error_code' => 0));
    }


    public function uploadImage()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            $this->ajaxReturn(array('code' => 1, 'msg' => $upload->getError()));
        } else {
            foreach ($info as $k => $v) {
                $real_path = $v['savepath'] . $v['savename'];
            }
            $this->ajaxReturn(array('code' => 0, 'msg' => '成功', 'data' => array('src' => 'http://www.layui.test/Uploads/' . $real_path, 'title' => '土拨鼠')));
        }
    }

    //图片懒加载
    public function layimg()
    {
        $opt = [];
        $page = I('page', 1);
        $pageSize = 6;
        $data = $this->role_user->getAllByLimit($opt, $page, $pageSize);
        $this->assign('data',$data['data']);
        $this->display('img');
    }

    public function layimg_callback()
    {
        $opt = [];
        $page = I('page', 1);
        $pageSize = 2;
        $data = $this->role_user->getAllByLimit($opt, $page, $pageSize);
        $this->ajaxReturn(array('error_code' => 0, 'data' => $data['data'],'pages'=>5));
    }

    public  function tree($data, $parent,$level = 0)
    {
        unset($list);
        static $list = array();
        foreach ($data as $key => $v) {
            if ($v['pid'] == $parent) {
                $list[] = array(
                    'value'=>$v['id'],
                    'level'=>$level,
                    'name'=>$v['name']
                );
                $this->tree($data, $v['id'],$level+1);
            }
        }

        return $list;
    }

    public  function trees($data, $parent,$level = 0)
    {

        static $lists = array();
        foreach ($data as $key => $v) {
            if ($v['pid'] == $parent) {
                $lists[] = array(
                    'value'=>$v['id'],
                    'level'=>$level,
                    'name'=>$v['name']
                );
                $this->trees($data, $v['id'],$level+1);
            }
        }

        return $lists;
    }

}